package common.Utils;

public class SimpleLock 
{
	private boolean lock;
	private String lockOwner = null;
	
	public boolean tryLock()
	{
		boolean retVal = false;
		
		StackTraceElement ste = Thread.currentThread().getStackTrace()[2];
		String currentMethod = ste.getClassName() + ste.getMethodName(); 
		if(lockOwner == null || currentMethod.equals(lockOwner))
		{
			lock = true;
			retVal = true;
			lockOwner = currentMethod;
		}
		
		return retVal;
	}
	
	public void unlock()
	{
		StackTraceElement ste = Thread.currentThread().getStackTrace()[2];
		String currentMethod = ste.getClassName() + ste.getMethodName(); 
		if(currentMethod.equals(lockOwner))
		{
			lock = false;
			lockOwner = null;
		}
	}
	
	public boolean isLocked()
	{
		return lock;
	}
}
